package com.xtck.ha.common.core.utils;

import com.alibaba.fastjson2.JSONObject;
import com.xtck.ha.common.core.annotation.DsFieldSpecialQuery;
import com.xtck.ha.common.core.annotation.IgnoreProperty;
import com.xtck.ha.common.core.annotation.TableName;
import com.xtck.ha.common.core.constant.DsConstants;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


public class DomainUtils {

    /**
     * 获取实体类对应的数据库表名
     * @param obj
     * @return
     */
    public static String getDomainTableName(Object obj) {
        JSONObject jo = new JSONObject();
        Class<?> clazz = obj.getClass();
        String tableName;
        // 检查类是否被@MyAnnotation注解
        if (clazz.isAnnotationPresent(TableName.class)) {
            // 获取类上的@MyAnnotation注解
            TableName annotation = clazz.getAnnotation(TableName.class);

            // 获取注解的属性值
            tableName = annotation.value();
        } else {
            tableName = camelCaseToUpperCaseUnderscore(clazz.getSimpleName());
        }
        return tableName;
    }

    /**
     * 实体对象字段值转化
     * @param obj
     * @return
     */
    public static Map<String, Object> objectToMapWithSnakeCaseKeys(Object obj) {
        Map<String, Object> jo = new HashMap<>();
        Field[] fields = obj.getClass().getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true); // 确保可以访问私有字段
            try {
                Object value = field.get(obj);
                if (value != null && !field.isAnnotationPresent(IgnoreProperty.class)) {
                    // 将字段名从驼峰命名法转为XX_XX模式
                    String key = camelCaseToUpperCaseUnderscore(field.getName());
                    jo.put(key, value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return jo;
    }

    /**
     * 实体对象字段转化（主要针对于查询操作）
     * @param obj
     * @return
     */
    public static Map<String, Object> objectToMapForQuery(Object obj) {
        Map<String, Object> jo = new HashMap<>();
        Field[] fields = obj.getClass().getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true); // 确保可以访问私有字段
            try {
                Object value = field.get(obj);
                if (value != null && !field.isAnnotationPresent(IgnoreProperty.class)) {
                    // 将字段名从驼峰命名法转为XX_XX模式
                    String key = camelCaseToUpperCaseUnderscore(field.getName());
                    if(field.isAnnotationPresent(DsFieldSpecialQuery.class)){
                        String annoValue = field.getAnnotation(DsFieldSpecialQuery.class).value();
                        JSONObject j = new JSONObject();
                        if(DsConstants.LIKE.equals(annoValue)){
                            j.put(annoValue,value+"%");
                        }else {
                            j.put(annoValue,value);
                        }
                        value = j;
                    }
                    jo.put(key, value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return jo;
    }

    // 方法：将驼峰命名法转为XX_XX模式
    public static String camelCaseToUpperCaseUnderscore(String str) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c) || Character.isDigit(c)) {
                if (i > 0) {
                    result.append("_");
                }
                result.append(Character.toUpperCase(c));
            } else {
                result.append(Character.toUpperCase(c));
            }
        }
        return result.toString();
    }

}
